#操作模拟器的插件
import sublime, sublime_plugin,os,sys,shutil
#view.run_command('opensimulator')
import subprocess 

globals()['simulatorPaths'] = []
ROOT_PTAH = "XTool"
def searchPath(path,name):
	print('path---',path)
	print('name---',name)
	for x in os.listdir(path):
		tpath = os.path.join(path,x)
		if os.path.isdir(tpath):
			searchPath(tpath,name)
		else:
			if x==name:
				globals()['simulatorPaths'].append(tpath)

#获取运行simulator个数
def get_process_count(name):
	p = os.popen(name) 
	return p.read().count(name) 


#运行拷贝模拟器脚本
class copysimulatorCommand(sublime_plugin.TextCommand):
	def run(self,edit):
		globals()['terrySettings'] = sublime.load_settings('DefaultDoC.sublime-settings')
		projectPath = globals()['terrySettings'].get('desPath','')
		retval = os.getcwd()	
		os.chdir(os.path.join(projectPath,'simulator'))
		scriptPath = os.path.join(sublime.packages_path(),ROOT_PTAH,"generate.bat")
		subprocess.Popen(scriptPath) 



class opensimulatorCommand(sublime_plugin.TextCommand):
	def run(self, edit,snum=0):
		globals()['terrySettings'] = sublime.load_settings('DefaultDoC.sublime-settings')
		projectPath = globals()['terrySettings'].get('desPath','')
		if not simulatorPaths:
			globals()['simulatorPaths'] = []
		if len(simulatorPaths)==0:
			searchPath(os.path.join(projectPath,'simulator'),"MahjongClient.exe")
		if len(simulatorPaths)==0:
			sublime.message_dialog("没能找到模拟器!请检查desPath配置路径")
		if snum>len(simulatorPaths)-1:
			sublime.message_dialog("没有改模拟器!请重新输入")
		print("simulatorPaths",simulatorPaths)
		dirs,p = os.path.split(simulatorPaths[snum])
		retval = os.getcwd()	
		os.chdir(dirs)
		#子进程启动  
		subprocess.Popen(simulatorPaths[snum] + " -resolution 826x520") 

#删除玩家默认配置
class deleteuserdataCommand(sublime_plugin.TextCommand):
	def run(self, edit,snum=0):
		print("ssssss")
		globals()['terrySettings'] = sublime.load_settings('DefaultDoC.sublime-settings')
		projectPath = globals()['terrySettings'].get('desPath','')
		if not simulatorPaths:
			globals()['simulatorPaths'] = []
		if len(simulatorPaths)==0:
			searchPath(os.path.join(projectPath,'simulator'),"MahjongClient.exe")
		if len(simulatorPaths)==0:
			sublime.message_dialog("没能找到模拟器!请检查desPath配置路径")
		if snum>len(simulatorPaths)-1:
			sublime.message_dialog("没有改模拟器!请重新输入")
		print("simulatorPaths",simulatorPaths)
		dirs,p = os.path.split(simulatorPaths[snum])
		defaultFile = os.path.join(dirs,"UserDefault.xml")
		print ('defaultFile',defaultFile)
		try:
			os.remove(defaultFile)
			#sublime.message_dialog("删除成功")
			self.view.window().status_message("删除成功"+defaultFile)
		except Exception as err:
			#sublime.message_dialog("删除失败,不存在该文件")
			self.view.window().status_message("删除失败,不存在该文件"+defaultFile)

		#if get_process_count(simulatorPaths[0]) == 0:
			#subprocess.Popen([r'D:\shuaji\bin\main.exe']) 
			#os.system('start '+);

		#获取引擎工程路径,找到模拟器路径
		# print(xcopyPath)
		# retval = os.getcwd()
		# os.chdir(xcopyPath)
		# bat = os.path.join(xcopyPath,'copyAll.bat');
		# print(bat)
		# if os.path.isfile(bat):
		# 	os.system('start '+bat);
		# os.chdir(retval)

#打开模拟器
class openconstantCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		globals()['settings'] = sublime.load_settings('DefaultDoC.sublime-settings')
		projectPath = globals()['terrySettings'].get('desPath','')

		file = os.path.join(projectPath,'simulator/win32/src/app/manager/Constants.lua')
		self.view.window().open_file(file)
		
